package com.snail.common.dyd;

import com.snail.common.dyd.bean.DataSourceInfo;
import com.snail.common.dyd.impl.AspectDataBaseTypeSwitcher;
import com.snail.common.dyd.impl.DataSourceIdFromAspectProvider;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.aspectj.lang.ProceedingJoinPoint;

@Data
@AllArgsConstructor
public class AspectDynamicDataBaseProcessor {


    private DataBaseOperatorProvider dataBaseOperatorProvider;

    private DataSourceSwitcher dataSourceSwitcher;

    private DataSourceInfoProvider dataSourceInfoProvider;

    public void switchDataBase(ProceedingJoinPoint proceedingJoinPoint) {

        // 1.获取数据源信息
        String dataSourceId = new DataSourceIdFromAspectProvider(proceedingJoinPoint).getDataSourceId();
        DataSourceInfo dataSourceInfo = getDataSourceInfoProvider().getDataSourceInfo(dataSourceId);

        // 2.切换数据源
        getDataSourceSwitcher().switchDataSource(dataSourceInfo);

        // 3.切换数据库类型操作者
        new AspectDataBaseTypeSwitcher(proceedingJoinPoint)
                .switchDataBaseType(getDataBaseOperatorProvider().getDataBaseOperator(dataSourceInfo.getDbType()));
    }


}
